import Vue from 'vue'
import Router from 'vue-router'
import { sync } from 'vuex-router-sync'
import store from '../vuex/store'

//路由懒加载，实为异步加载路由，避免单个app.js体积过大
/*import home from '@/template/home'
 import detail from '@/template/detail'*/
const home = r => require.ensure([], () => r(require('@/template/home')), 'group-a')
const itservice = r => require.ensure([], () => r(require('@/template/ITservice')), 'group-a')
const processproblem = r => require.ensure([], () => r(require('@/template/processproblem')), 'group-a')
const progressquery = r => require.ensure([], () => r(require('@/template/progressquery')), 'group-a')
const complain = r => require.ensure([], () => r(require('@/template/complain')), 'group-a')
const systemlinkman = r => require.ensure([], () => r(require('@/template/systemlinkman')), 'group-a')
const operationlinkman = r => require.ensure([], () => r(require('@/template/operationlinkman')), 'group-a')
const passwordreset = r => require.ensure([], () => r(require('@/template/passwordreset')), 'group-a')
const problemdetail = r => require.ensure([], () => r(require('@/template/problemdetail')), 'group-a')
Vue.use(Router)

const router = new Router({
    routes: [{
            path: '/',
            name: 'home',
            component: home,
            keepAlive: true
        },
        {
            path: '/itservice',
            name: 'itservice',
            component: itservice
        },
        {
            path: '/processproblem',
            name: 'processproblem',
            component: processproblem
        },
        {
            path: '/progressquery',
            name: 'progressquery',
            component: progressquery,
            // children: [{
            //     path: '/problemdetail:id', //需要加对应的id
            //     name: 'problemdetail',
            //     component: problemdetail
            // }]
        },
        {
            path: '/complain',
            name: 'complain',
            component: complain
        },
        {
            path: '/systemlinkman',
            name: 'systemlinkman',
            component: systemlinkman
        },
        {
            path: '/operationlinkman',
            name: 'operationlinkman',
            component: operationlinkman
        },
        {
            path: '/passwordreset',
            name: 'passwordreset',
            component: passwordreset
        },
        {
            path: 'todetail/problemdetail/:id',
            name: 'problemdetail',
            component: problemdetail
        },
    ]
})

sync(store, router)

const history = window.sessionStorage
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)

router.beforeEach(function(to, from, next) {
    store.commit('updateLoading', true)
    Vue.$vux.loading.show();

    const toIndex = history.getItem(to.path)
    const fromIndex = history.getItem(from.path)

    if (toIndex) {
        if (toIndex > fromIndex || !fromIndex || (toIndex === '0' && fromIndex === '0')) {
            store.commit('updateDirection', 'forward')
        } else {
            store.commit('updateDirection', 'reverse')
        }
    } else {
        ++historyCount
        history.setItem('count', historyCount)
        to.path !== '/' && history.setItem(to.path, historyCount)
        store.commit('updateDirection', 'forward')
    }

    if (/\/http/.test(to.path)) {
        let url = to.path.split('http')[1]
        window.location.href = `http${url}`
    } else {
        next()
    }
})

router.afterEach(function(to) {
    store.commit('updateLoading', false)
    Vue.$vux.loading.hide();
})

export default router